//
//  ByWebViewController.m
//  beibei_ios
//
//  Created by mc on 16/5/31.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByWebViewController.h"

@interface ByWebViewController ()<UIScrollViewDelegate>
{
    UILabel *_loadLabel; // 网页加载提示文字
    UIActivityIndicatorView *_indicatorView; // 网页加载菊花转动
    UILabel *_notesLable;//提供者
}
@end

@implementation ByWebViewController

// 自定义初始化方法，获取所要显示的网页的网址，并显示该网页
- (instancetype)initWithUrlString:(NSString *)urlString {
    self = [super init];
    if (self) {
    
        // 创建网页webView
        CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV - 44);
        _webView = [[UIWebView alloc] initWithFrame:frame];
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
        _webView.scrollView.delegate = self;
        [self.view addSubview:_webView];
        
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV - 44)];
        backgroundView.backgroundColor = rgba(46, 49, 50, 1);
        [_webView insertSubview:backgroundView atIndex:0];
        
        _notesLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 20)];
        _notesLable.text = @"网页由 博也 提供";
        _notesLable.textAlignment = NSTextAlignmentCenter;
        _notesLable.textColor = [UIColor grayColor];
        _notesLable.backgroundColor = [UIColor clearColor];
        _notesLable.font = FONT(12);
        _notesLable.hidden = YES;
        [backgroundView addSubview:_notesLable];
        [_webView insertSubview:backgroundView atIndex:0];
       
        
        // 发送请求，获取该网页内容
        urlString = [[NSString stringWithFormat:@"%@%@",BOYE_WEB_BASE_URL,urlString] stringByEncodingURLFormat];
        NSLog(@"urlString:%@", urlString);
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [_webView loadRequest:request];
    }
    return self;
}

- (instancetype)initWithTotalUrlString:(NSString *)urlString {
    self = [super init];
    if (self) {
        
        // 创建网页webView
        CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV - 44);
        _webView = [[UIWebView alloc] initWithFrame:frame];
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
        _webView.scrollView.delegate = self;
        [self.view addSubview:_webView];
        
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV - 44)];
        backgroundView.backgroundColor = rgba(46, 49, 50, 1);
        [_webView insertSubview:backgroundView atIndex:0];
        
        _notesLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 20)];
        _notesLable.text = @"网页由 博也 提供";
        _notesLable.textAlignment = NSTextAlignmentCenter;
        _notesLable.textColor = [UIColor grayColor];
        _notesLable.backgroundColor = [UIColor clearColor];
        _notesLable.font = FONT(12);
        _notesLable.hidden = YES;
        [backgroundView addSubview:_notesLable];
        [_webView insertSubview:backgroundView atIndex:0];
        
        
        // 发送请求，获取该网页内容
        urlString = [[NSString stringWithFormat:@"%@",urlString] stringByEncodingURLFormat];
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [_webView loadRequest:request];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBarButtonItem];
    
}
- (void)setTitleString:(NSString *)titleString {
    
    _titleString = titleString;
    self.title = _titleString;

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    [self.ljwKeyboardHandler stopHandling];
}


#pragma mark - UIVebView代理方法
// 开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    // 加载网页时，菊花转动
    _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _indicatorView.color = [UIColor lightGrayColor];
    CGPoint point = CGPointMake(_webView.width / 2, _webView.height / 2);
    _indicatorView.center = point;
    [_webView addSubview:_indicatorView];
    _indicatorView.hidesWhenStopped = YES;
    [_indicatorView startAnimating];
    // 10秒后还未加载完就隐藏菊花
    [self performSelector:@selector(delayAction) withObject:nil afterDelay:5];
}

- (BOOL)webView: (UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *url = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSLog(@"网址事件 = %@", url);
    return YES;
}
- (void)delayAction {
    
    [_indicatorView stopAnimating];
}

// 加载结束
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    // 加载提示移除
    [_indicatorView stopAnimating];
}


#pragma mark - UIScrollViewDelegate代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat yOffset = scrollView.contentOffset.y;
    //向上偏移量变正  向下偏移量变负
    if (yOffset < 0) {
        _notesLable.hidden = NO;
    }else{
        _notesLable.hidden = YES;
    }
    
}


#pragma mark - 返回上一界面
- (void)leftItemsAction:(NSInteger)leftIndex {
    if (_webView.canGoBack) {
        [_webView goBack];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}





@end
